﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KojackGames.Blackjack.Domain.GamePlay.Model.Exceptions;
using KojackGames.Blackjack.Domain.GamePlay.Model.PlayingPosition;
using KojackGames.Blackjack.Domain.GamePlay.Model.PlayingPosition.Hands.Status;
using KojackGames.Blackjack.Domain.Membership.Model;

namespace KojackGames.Blackjack.Domain.GamePlay.Model.Dealer.Actions
{
    public class DoubleDown : DealerActionOnPlayersHand, IDealerAction
    {
        public DoubleDown(IHandStatusFactory hand_status_factory, IPlayDealersHand play_dealers_hand, 
                         IPlayingHandsEndGameStatusDecider playing_hands_end_game_status_decider,
                         IChipAllocator chip_allocator)
            : base(hand_status_factory, play_dealers_hand, playing_hands_end_game_status_decider, chip_allocator)
        {            
        }
    
        public override void action_to_perform(IPlayingPositions playing_positions, ICardShoe card_shoe, IPlayer player)
        {
            raise_illegal_move_if_action_cannot_be_made_on(playing_positions, player);
            player.decrease_pot_by(playing_positions.players_active_hand.wager);
            playing_positions.players_active_hand.double_stake();

            playing_positions.players_active_hand.add(card_shoe.take_card());            
            playing_positions.players_active_hand.change_state_to(HandStatus.stick);
        }

        public void raise_illegal_move_if_action_cannot_be_made_on(IPlayingPositions playing_positions, IPlayer player)
        {
            if (!player.can_afford_to_bet(playing_positions.players_active_hand.wager))
                throw new IllegalMoveException(string.Format("Please cash in to Double Down. You need at least {0} dollars.", playing_positions.players_active_hand.wager.ToString()));            
        }
        
    }
}
